﻿namespace MicroCloud.Options
{
    /// <summary>
    /// 电子邮件配置
    /// </summary>
    public class EmailOption
    {
        #region 属性
        /// <summary>
        /// 获取或设置 发送功能配置
        /// </summary>
        public EmailSenderOptions Sender { get; set; } = new EmailSenderOptions();

        /// <summary>
        /// 获取或设置 接收功能配置
        /// </summary>
        public EmailReceiverOptions Receiver { get; set; } = new EmailReceiverOptions();

        /// <summary>
        /// 获取或设置 自动接收间隔(分钟)，0表示不启用自动接收。
        /// </summary>
        public int ReceiveInterval { get; set; }

        #endregion

    }

     /// <summary>
    /// 邮件发送配置信息
    /// </summary>
    public class EmailSenderOptions
    {
        #region 属性
        /// <summary>
        /// 获取或设置 邮件发送服务器
        /// </summary>
        public string Host { get; set; }

        #region 获取或设置 端口号
        private int _Port;
        /// <summary>
        /// 获取或设置 端口号：非SSL安全协议一般默认25，SSL安全协议一般默认465，具体查询对应的邮件服务系统。
        /// </summary>
        public int Port
        {
            get
            {
                if (_Port <= 0)
                {
                    _Port = EnableSsl ? 465 : 25;
                }
                return _Port;
            }
            set { _Port = value; }
        }
        #endregion

        /// <summary>
        /// 获取或设置 是否SSL
        /// </summary>
        public bool EnableSsl { get; set; }

        /// <summary>
        /// 获取或设置 发送方显示名
        /// </summary>
        public string DisplayName { get; set; }
        
        /// <summary>
        /// 获取或设置 发送方标题前缀
        /// </summary>
        public string SubjectPrefix { get; set; }

        /// <summary>
        /// 获取或设置 发送方用户名
        /// </summary>
        public string UserName { get; set; }

        /// <summary>
        /// 获取或设置 发送方秘钥
        /// </summary>
        public string Password { get; set; }

        #endregion

        #region 方法
        /// <summary>
        /// 获取邮件协议类型
        /// </summary>
        /// <returns>邮件协议类型</returns>
        public EmailProtocolType GetProtocolType()
        {
            var protocolType = EmailProtocolType.UNKNOWN;
            var hostLower = Host.ToLower();
            if (hostLower.StartsWith("smtp")) { protocolType = EmailProtocolType.SMTP; }
            else if (hostLower.StartsWith("pop")) { protocolType = EmailProtocolType.POP; }
            else if (hostLower.StartsWith("imap")) { protocolType = EmailProtocolType.IMAP; }
            return protocolType;
        }

        #endregion

    }


    /// <summary>
    /// 邮件接收配置信息
    /// </summary>
    public class EmailReceiverOptions
    {
        #region 属性
        /// <summary>
        /// 获取或设置 邮件接收服务器
        /// </summary>
        public string Host { get; set; }

        #region 获取或设置 端口号
        private int _Port;
        /// <summary>
        /// 获取或设置 端口号：pop一般默认(非SSL: 110、SSL: 995)，imap一般默认(非SSL: 143、SSL: 993)，具体查询对应的邮件服务系统。
        /// </summary>
        public int Port
        {
            get
            {
                if (_Port <= 0)
                {
                    var protocolType = GetProtocolType();
                    if (protocolType == EmailProtocolType.POP)
                    {
                        _Port = EnableSsl ? 995 : 110;
                    }
                    else
                    {
                        _Port = EnableSsl ? 993 : 143;
                    }
                }
                return _Port;
            }
            set { _Port = value; }
        }
        #endregion
        
        /// <summary>
        /// 获取或设置 是否SSL
        /// </summary>
        public bool EnableSsl { get; set; }
        
        /// <summary>
        /// 获取或设置 发送方用户名
        /// </summary>
        public string UserName { get; set; }

        /// <summary>
        /// 获取或设置 接收方秘钥
        /// </summary>
        public string Password { get; set; }

        #endregion

        #region 方法
        /// <summary>
        /// 获取邮件协议类型
        /// </summary>
        /// <returns>邮件协议类型</returns>
        public EmailProtocolType GetProtocolType()
        {
            var protocolType = EmailProtocolType.UNKNOWN;
            var hostLower = Host.ToLower();
            if (hostLower.StartsWith("smtp")) { protocolType = EmailProtocolType.SMTP; }
            else if (hostLower.StartsWith("pop")) { protocolType = EmailProtocolType.POP; }
            else if (hostLower.StartsWith("imap")) { protocolType = EmailProtocolType.IMAP; }
            return protocolType;
        }

        #endregion

    }

}
